<template>
  <div class='report first-box'>
    <Form :formData="formData" :formParam="formParam"></Form>
    <Btn class="margin40" :buttonData="buttonData"></Btn>

    <DetailAlert :alertParams="alertParams"></DetailAlert>
    <p style="display:none">{{getCheckMenu}}</p>
  </div>
</template>
<script>
import Form from '../../common/form'
import Btn from '../../common/button.vue'
import DetailAlert from './detailalert'
export default {
  components: {Form,Btn,DetailAlert},
  name: "",
  data() {
    return {
      alertParams:{},//编辑的内容
      formData:[
        {
          title:{
            show:false,
          },
          children:[
            {type:"select",label:"专项检测",key:"detectionId",array:[]},
            {type:"number",label:"检测项数",key:"detectionNum",},
            {type:"input",label:"检测项目",key:"",placeholder:" ",readonly:true},
            {type:"menulist",array:[],click:this.showCheckDetail}
          ],
        }
      ],
      formParam:{
        detectionerId:"",
        detectionId:"",
        detectionNum:"",
        data:[],
      },

      buttonData:[
        {
          label:"保存",
          click:this.save
        }
      ]
    };
  },
  computed:{
    getCheckMenu(){
      if(!this.formParam.detectionId)return
      var params={
        id:this.formParam.detectionId
      }
      this.getDefault(params)
    }
  },
  methods: {
    save(){
      var params=JSON.parse(JSON.stringify(this.formParam))
      params.data.forEach(ele=>{
        ele.isService=ele.isService==true?1:0
      })
      params.data=JSON.stringify(params.data)
      _g.apiPost("pickUp/saveReport.do",params).then(res=>{
        _g.toMessage({status:res.status==0?"0":"1",msg:res.status==0?"操作成功":"操作失败"})
        if(!res.status){
          sessionStorage.setItem("reportDoneId",res.data.id)
          setTimeout(()=>{
            this.$router.go(-1)
          },300)
        }
      })
    },

    getSelectData(){
      _g.apiPost("pickUp/selectDetection.do").then(res=>{
        if(res.status)return
        var goal=[]
        res.data.forEach(ele => {
          goal.push(
            {
              key:ele.id,
              value:ele.name
            }
          )
        });
        this.$set(this.formData[0].children[0],"array",goal)
      }).then(()=>{
        if(this.formData[0].children[0].array)this.formParam.detectionId=this.formData[0].children[0].array[0].key
      })
    },

    getDefault(params){
      _g.apiPost("pickUp/selectProject.do",params).then(res=>{
        this.formParam.detectionNum=res.data.count
        var goals=[]
        var goals2=[]
        res.data.list.forEach(ele=>{
          goals.push({
            label:ele.name,
            detectionName:ele.name,
            detectionStatus:"1",
            advice:"1",
            price:"",
            isService:false,
            serviceStatus:"",
            picture:"",
            days:"",
          })
          goals2.push({
            detectionName:ele.name,
            detectionStatus:"1",
            advice:1,
            price:"",
            isService:false,
            serviceStatus:"",
            picture:[],
            days:"",
          })
        })
        this.$set(this.formData[0].children[3],"array",goals)
        this.$set(this.formParam,"data",goals2)
      })

    },

    // 编辑检修内容
    showCheckDetail(item,index){
      this.alertParams=this.formParam.data[index]
      this.$store.state.dialogs.reportalert.show=true
    }
  },
  created() {
    this.formParam.detectionerId=_g.getRouterParams(this).id
    this.getSelectData()
  }
};
</script>
<style lang="scss" scoped>
.report {

  .margin40{
    margin: 40px 30px;
  }
}
</style>